package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.CompanyController;
import sg.edu.nus.iss.ooad.jass.entity.Company;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * This is Company List Form. The form is to allow the advertiser to keep a list of the companies. 
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0   
 *
 */
public class AdvertiserListCompanyForm extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = 7766848079644016948L;
        private final static Logger logger = Logger.getLogger(AdvertiserListCompanyForm.class.getName());
        //Form components
        private AbstractJASSRegularWindow parent = null;
        private Panel buttonPanel = null;
        private JASSRegularPanel formPanel = null;
        private List list = null;
        private Button buttonSelect = null;
        private Button buttonCancel = null;
        public String companyName = "";
        //Singleton method
        public static AdvertiserListCompanyForm instance;
        ArrayList<Company> companies ;

        /**
         * Constructor AdvertiserListCompanyForm.
         * @param parent
         */
        private AdvertiserListCompanyForm(AbstractJASSRegularWindow parent) {
                super("Job Advertisement and Search System", 150, 400);
                this.parent = parent;
                setResizable(false);
                initComponents();
                loadCompanyList();
        }

        /**
         * Singleton method to create AdvertiserListCompanyForm object.
         * @param parent
         * @return
         */
        public static AdvertiserListCompanyForm getInstance(AbstractJASSRegularWindow parent) {
                if (instance == null) {
                        instance = new AdvertiserListCompanyForm(parent);
                }
                return instance;
        }

        /**
         * Init Form Components
         */
        private void initComponents() {
                // initialize the components
                buttonSelect = new Button("Select");
                buttonSelect.addActionListener(new SelectActionListener());

                buttonCancel = new Button("Cancel");
                buttonCancel.addActionListener(new CancelActionListener());

                buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER, 5, 5));
                buttonPanel.add(buttonSelect);
                buttonPanel.add(buttonCancel);

                list = new List(10, false);
                formPanel = new JASSRegularPanel("Company List", 120, 360);
                formPanel.add(list, BorderLayout.CENTER);
                formPanel.add(buttonPanel, BorderLayout.SOUTH);

                //add the panel
                this.add(formPanel);
                pack();
        }

        /**
         * Load Company List.
         */
        private void loadCompanyList() {
                try {
                        CompanyController companyController = new CompanyController();
                         companies = (ArrayList<Company>) companyController.getAllCompany();
                        for (Company item : companies) {
                                list.add( item.getName() );
                                

                        }
                } catch (Exception e) {
                        logger.log(Level.SEVERE, null, e);
                }
        }

        /**
         *  This function is to attach the component to the specified layout of the panel. 
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */

        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * This is to reset form.
         */

        @Override
        public void resetForm() {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         * Select Action Handler. It is called when the user click button "Select"
         */
        class SelectActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        // throw new UnsupportedOperationException("Not supported yet.");
                        Company object = companies.get(list.getSelectedIndex());

                        CompanyController companyController = new CompanyController();
                        try {
                                Company c = companyController.retrieveCompany(object.getID());
                                if (parent instanceof AdvertiserUpdateJobAdvertisementForm) {
                                        AdvertiserUpdateJobAdvertisementForm form = (AdvertiserUpdateJobAdvertisementForm) parent;
                                        form.setCompanyId(c.getID());
                                        form.setCompanyName(c.getName());
                                } else if (parent instanceof AdvertiserCreateJobAdvertisementForm) {
                                        AdvertiserCreateJobAdvertisementForm form = (AdvertiserCreateJobAdvertisementForm) parent;
                                        form.setCompanyId(c.getID());
                                        form.setCompanyName(c.getName());
                                }
                                AdvertiserListCompanyForm.this.setVisible(false);
                        } catch (Exception ex) {
                                logger.log(Level.SEVERE, null, ex);
                        }

                }
        }

        /**
         * Cancel Action Handler. It is called when the user click button "Cancel"
         */
        class CancelActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        close();
                }
        } 
       
}
